<template>
	<view class="container">
		<view class="personal_top">
			<text class="personal_top_text1">基本资料</text>
			<view class="personal_border">
				<view class="personal_border_flex personal_wh">
					<text class="personal_border_text1">头像</text>
					<view class="personal_border_text2">
						<image @tap="uploadFilePromise()" :src="user.head_pic" mode="" class="personal_border_img1"></image>
					</view>
					<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/drop-down2.png" mode="" class="personal_border_img2"></image>
				</view>
				<view class="personal_border_flex" @click="nicknameto">
					<text class="personal_border_text1">昵称</text>
					<view class="personal_border_text2">
						<text>{{user.nickname}}</text>
					</view>
					<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/drop-down2.png" mode="" class="personal_border_img2"></image>
				</view>
			</view>
		</view>
		<view class="personal_top">
			<text class="personal_top_text1">我的信息</text>
			<view class="personal_border">
				<view class="personal_border_flex">
					<text class="personal_border_text1">手机号码</text>
					<view class="personal_border_text2">
						<text v-if="user.mobile">{{user.mobile}}</text>
						<text v-else style="color: #FF4362;">未设置</text>
					</view>
				</view>
				<view class="personal_border_flex" @click="certificationto">
					<text class="personal_border_text1">个人信息</text>
					<view class="personal_border_text2">
						<text v-if="user.is_ws==1">已完善</text>
						<text v-else style="color: #FF4362;">未完善</text>
					</view>
					<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/drop-down2.png" mode="" class="personal_border_img2"></image>
				</view>
			</view>
		</view>
		<SimpleDateTimePicker ref="myPicker" @submit="handleSubmit" :start-year="2000" :end-year="2030" />
		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm"
			themeColor="#007AFF" cancelColor="#525252"></simple-address>
	</view>
</template>

<script>
	import SimpleDateTimePicker from "uni_modules/buuug7-simple-datetime-picker/components/buuug7-simple-datetime-picker/buuug7-simple-datetime-picker.vue";
	import simpleAddress from '@/components/simple-address/simple-address.vue';
	import md5 from '@/components/MD5.js'
	var _this
	export default {
		data() {
			return {
				user:{},
				birthday: "",
				cityPickerValueDefault: [0, 0, 1],
				pickerText: '',
				address: '',
				fileList: [],
			}
		},
		components: {
			SimpleDateTimePicker,
			simpleAddress,
		},
		onLoad() {
			_this=this
		},
		onShow() {
			this.get_user_info()
		},
		methods: {
			//获取用户信息
			async get_user_info(){
				// post请求
				const data = await _this.$post('api/UserInfo')
				if(data.code==200){
					this.user=data.data.user
				}else{
					this.$u.toast(data.msg)
				}
			},
			//跳转到修改昵称页面
			nicknameto() {
				uni.navigateTo({
					url:'nickname?nickname='+this.user.nickname
				})
			},
			//跳转到个人信息
			certificationto() {
				this.$u.route({
					url: 'pages/center/personal/user_info'
				})
			},
			//跳转到银行卡
			bankcardto() {
				this.$u.route({
					url: 'pages/center/personal/bankcard2'
				})
			},
			// 打开picker
			openDatetimePicker() {
				this.$refs.myPicker.show();
			},

			// 关闭picker
			closeDatetimePicker() {
				this.$refs.myPicker.hide();
			},

			handleSubmit(e) {
				// {year: "2019", month: "07", day: "17", hour: "15", minute: "21"}
				// console.log(e);
				this.birthday = `${e.year}-${e.month}-${e.day}`;
			},
			openAddres() {
				this.cityPickerValueDefault = [0, 0, 1]
				this.$refs.simpleAddress.open();
			},
			onConfirm(e) {
				this.address = e.label
				this.pickerText = JSON.stringify(e);
				console.log(this.pickerText);
			},
			//图片上传
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise() {
				let sign_prefix=getApp().globalData.sign_prefix
				let sign=md5.hax_md5('pathhead_pic'+'user_id'+this.user_id+ sign_prefix + this.user_token + sign_prefix).toLocaleLowerCase()
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function (res) {
						uni.uploadFile({
							url: _this.baseUrl+'/api/UploadPic', // 仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							header:{
								token:_this.user_token
							},
							formData: {
								user_id: _this.user_id,
								path: 'head_pic',
								sign: sign
							},
							success: (result) => {
								let data=JSON.parse(result.data)
								if(data.code==200){
									_this.user.head_pic=data.data.img
								}else{
									uni.$u.toast(data.msg)
								}
							}
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		padding: 20rpx 0 0 0;
	}
	.personal_top {
		padding:0 20rpx 20rpx 20rpx;
		.personal_top_text1 {
			color: #adadad;
			margin-bottom: 20rpx;
			display: block;
		}
		.personal_border {
			border-radius: 20rpx;
			padding: 0 20rpx;
			background: #fff;
			box-shadow: $global-shade;
			.personal_border_text1 {
				width: 20%;
			}
			.personal_border_text2 {
				flex: 1;
			}
			.personal_border_flex {
				display: flex;
				align-items: center;
				min-height: 90rpx;
				&:not(:last-child){
					border-bottom: 1px solid #f3f3f3;
				}
			}
			.personal_wh{
				min-height: 120rpx;
			}
			.personal_border_img1 {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
			}
			
			.personal_border_img2 {
				width: 10rpx;
				height: 18rpx;
			}
		}
	}
</style>
